package com.hkemmm.controller;


import com.hkemmm.bean.AjaxRes;
import com.hkemmm.bean.AuthUser;
import com.hkemmm.common.service.RedisServer;
import com.hkemmm.service.IAuthUserService;
import com.hkemmm.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/auth", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class LoginController extends BaseController<LoginController> {
    @Autowired
    private IAuthUserService authUserService;
    @Autowired
    private RedisServer redisServer;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public AjaxRes login(AuthUser authUser) {
        AjaxRes ar = new AjaxRes();
        try {
            AuthUser one = authUserService.getUser(authUser);
            if (one != null) {
                String token = JWTUtil.sign(authUser);
                Map<String, Object> userMap = new HashMap<>();
                userMap.put("user", one);
                userMap.put("token", token);

                redisServer.set("user",token,60l*60l);
                ar.setSucceed(userMap);
                ar.setResMsg("登陆成功");
            }

        } catch (Exception e) {
            ar.setResMsg("登陆失败");
            System.out.println(e.getMessage());
        }
        return ar;
    }

}
